<!--#include file="functions.asp" -->
<%

dim sql
dim i
dim NextVerNum
    NextVerNum=0
	
sql = "SELECT  A.GROUPDESC, A.EMPCODE, A.MANAGERNAME"
sql = sql & " FROM dbo.EXIAUTHORITY A"
sql = sql & " WHERE A.DOATO= '" & e & "' AND ISDOA='Y'"

set rsDOA=cn.execute(sql)

'set next location
NextLocation=request("reqEndUser")  'set nextlocation to end user at the first time
	  	
'update exiverify
'if (request("MultiUser")="Y") and (request("isUC")="Y") then 'previous
if (request("MultiUser")="Y") then
  For i = 1 to Cint(request("Incr2"))-1
    if (request("VerNum"&i)<>"0") then
	  if (request("status"&i)<>"VERIFIED")then
	  sql = "UPDATE EXIVERIFY SET "
	  sql = sql & " VERIFIEDBY='" & request("Ver"&i) & "',"
	  sql = sql & " VERNAME='" & request("VerName"&i) & "'"
	  sql = sql & " WHERE SEQNUM = " & request("VerNum"&i)
	  cn.execute(sql)
	  end if
	else
	  if (request("Ver"&i)<>"") then
	    sql = "INSERT INTO EXIVERIFY (INVSEQNUM,VERIFIEDBY,VERNAME) VALUES ("
		sql = sql & request("Seqnum") & ",'"
		sql = sql & request("Ver"&i) & "','"
		sql = sql & request("VerName"&i) & "')"
		cn.execute(sql)
	  end if
	end if
  next
  if (request("verstat")="VERIFIED") or (request("UC")="") then
  'if (request("TrType")="Verify") then
	    sql = "SELECT ISNULL(MIN(SEQNUM),0) FROM EXIVERIFY WHERE INVSEQNUM=" & request("seqnum")
        sql = sql & " AND VERIFYSTATUS='NOT VERIFY'"
        set rs = cn.execute(sql)
	    NextVerNum=rs(0)
	    set rs=nothing
	  
	    if (NextVerNum<>0) then
	      sql = "SELECT VERIFIEDBY FROM EXIVERIFY WHERE SEQNUM="& NextVerNum
	      set rs=cn.execute(sql)
	      NextLocation= rs("VERIFIEDBY")
	      set rs=nothing
	    else
	     'NextLocation=request("reqEndUser")
	      NextLocation=request("reqApproved")
	    end if
   else
	   NextLocation=request("reqEndUser") 
   end if
end if

if (request("TrType")="MultiVerify") and (request("hf")="a") then
      sql = "UPDATE EXIVERIFY SET "
	  sql = sql & " DATEVERIFIED=GETDATE(),"
	  sql = sql & " VERIFYSTATUS='VERIFIED'"
	  sql = sql & " WHERE SEQNUM = " & request("VSeqnum")
	  cn.execute(sql)
	  
	  sql = "SELECT MIN(SEQNUM)FROM EXIVERIFY WHERE INVSEQNUM=" & request("seqnum")
      sql = sql & " AND VERIFYSTATUS='NOT VERIFY'"
      set rs = cn.execute(sql)
	  NextVerNum=rs(0)
	  set rs=nothing
	  
	  if (NextVerNum<>0) then
	  sql = "SELECT VERIFIEDBY FROM EXIVERIFY WHERE SEQNUM="& NextVerNum
	  set rs=cn.execute(sql)
	  NextLocation= rs("VERIFIEDBY")
	  else
	  'NextLocation=request("reqEndUser")
	  NextLocation=request("reqApproved")
	  end if
end if

'update exireg
  	sql = "UPDATE EXIREG SET "
	sql = sql & " VERIFIEDBY = '" & request("reqEndUser") & "',"
	sql = sql & " AUTHORISEDBY = '" & request("reqAuthorised") & "',"
	sql = sql & " APPROVEDBY = '" & request("reqApproved") & "',"	
    if (request("isUC")="Y") then
	  if (request("refOrder")="Y") then 
	  sql = sql & "PONUM='"& request("reqPONum") & "',"
	  end if 	  
	end if
	  if (request("MultiUser")="Y") then
	  	sql = sql & " MULTIVERIFY = 'Y',"   
	  else
		sql = sql & " MULTIVERIFY = 'N'," 
	  end if
	  if (request("JoinAppr")="Y") then
	  	sql = sql & " JOINAPPROVAL = 'Y',"  
		sql = sql & " APPROVED2BY = '" & request("Approved2") & "',"
	  else
		sql = sql & " JOINAPPROVAL = 'N'," 
	  end if
	
'-------------------------update EXIREG based on transaction type

    if (request("TrType")="MultiVerify") and (request("hf")="a") then
	    sql = sql & " LOCATION = '" & NextLocation & "',"
		sql = sql & " INVSTATUS='ROUTED',"
    end if
	if (request("TrType")="Verify") and (request("hf")="a") then
	 	sql = sql & " VERIFIEDSTATUS='VERIFIED',"
		sql = sql & " DATEVERIFIED=GETDATE(),"
		if (request("MultiUser")="Y") then
		sql = sql & " LOCATION = '" & request("Ver1") & "',"
	    else
		sql = sql & " LOCATION = '" & request("reqApproved") & "',"
		end if
		sql = sql & " INVSTATUS='ROUTED',"
	end if
	if (request("TrType")="Approve") and (request("hf")="a") then		
		if (request("refOrder")="Y") then		
		sql = sql & " INVSTATUS='APPROVED W/O RECEIPT',"
		else
		sql = sql & " INVSTATUS='APPROVED',"
	    end if
		sql = sql & " DATEAPPROVED=GETDATE(),"
		sql = sql & " DATEAUTHORISED=GETDATE(),"
		if (request("JoinAppr")="Y") then
		sql = sql & " LOCATION = '" & request("Approved2") & "',"
		else
		   if (request("refOrder")="Y") then 
		   sql = sql & " LOCATION = '" & request("UC") & "',"
		   else
		   sql = sql & " LOCATION = 'FINANCE',"
		   end if
		end if			
	end if
	if (request("TrType")="Approve2") and (request("hf")="a") then		
		if (request("refOrder")="Y") then		
		sql = sql & " INVSTATUS='APPROVED W/O RECEIPT',"
		else
		sql = sql & " INVSTATUS='APPROVED',"
	    end if
		sql = sql & " DATEAPPROVED2=GETDATE(),"
		if (request("refOrder")="Y") then 
		sql = sql & " LOCATION = '" & request("UC") & "',"
		else
		sql = sql & " LOCATION = 'FINANCE',"
		end if			
	end if
	if (request("TrType")="PreApprove") and (request("hf")="a") then	
		sql = sql & " INVSTATUS='PREAPPROVED',"
		sql = sql & " DATEAPPROVED=GETDATE(),"
		if (request("JoinAppr")="Y") then
		sql = sql & " LOCATION = '" & request("Approved2") & "',"
		else
		sql = sql & " LOCATION = '" & request("reqAuthorised") & "',"
		end if
	end if
	if (request("TrType")="PreApprove2") and (request("hf")="a") then	
		sql = sql & " INVSTATUS='PREAPPROVED',"
		sql = sql & " DATEAPPROVED2=GETDATE(),"
		sql = sql & " LOCATION = '" & request("reqAuthorised") & "',"
	end if
	if (request("TrType")="Route") and (request("hf")="a") then
	    if (request("MultiUser")="Y") then
		sql = sql & " LOCATION = '" & NextLocation & "',"
	    else
		sql = sql & " LOCATION = '" & request("reqEndUser") & "',"
		end if
		sql = sql & " INVSTATUS='ROUTED',"
	end if
'----------------------------------------------------------------------
	if (request("Comments")<>"") then
    txComments = dblq(request("Comments1")) & Chr(13)
	txComments = txComments & ">" & UCase(UsrFullname) & "(" & now &"):  " 
	txComments = txComments & dblq(request("Comments")) & Chr(13)
	sql = sql & " COMMENTS= '" & txComments & "',"
    end if
	sql = sql & " UPDATEUSER='" & usrName & "',"
	sql = sql & " UPDATESTAMP=GETDATE()" 
	sql = sql & " WHERE SEQNUM=" & request("seqnum")

	cn.execute(sql)


'update exicharge
'if (request("isUC")="Y") then
    For i = 1 to CInt(request("Incr"))-1
	if (request("seqnum"&i) <> "0") then
	  sql = "UPDATE EXICHARGE SET "
	  sql = sql & "ACCTCODE='" & request("ActCode"&i) & "',"
	  sql = sql & "T0CODE='" & request("T0"&i) & "',"
	  sql = sql & "T1CODE='" & request("T1"&i) & "',"
	  sql = sql & "T2CODE='" & request("T2"&i) & "',"
	  sql = sql & "T3CODE='" & request("T3"&i) & "',"
	  sql = sql & "AMOUNT=" & delkoma(request("Amt"&i)) & " "
	  sql = sql & " WHERE SEQNUM=" & request("seqnum"&i)
	  cn.execute(sql)
	else
	  if (request("ActCode"&i) <> "") or (request("T0"&i)<>"") or (request("T1"&i)<>"") or (request("T3"&i)<>"")  then
	    sql = "INSERT INTO EXICHARGE(INVSEQNUM,ACCTCODE,T0CODE,"
	  	sql = sql & "T1CODE, T2CODE, T3CODE, AMOUNT) VALUES ("
	  	sql = sql & request("seqnum") & ",'"
	  	sql = sql & request("ActCode"&i) & "','"
	  	sql = sql & request("T0"&i) & "','"
	  	sql = sql & request("T1"&i) & "','"
	  	sql = sql & request("T2"&i) & "','"
	  	sql = sql & request("T3"&i) & "',"
	  	sql = sql & delkoma(request("Amt"&i)) & ")"
	    cn.execute(sql)
	  end if
	end if
	
	Next
'end if

if (request("hf")="a") then
'update exihist
	sql = "INSERT INTO EXIHIST (SEQNUM,TRANSTYPE,LOCATION,DEPARTMENT,DATE,UPDATEUSER,UPDATESTAMP)"
  	sql = sql & " VALUES ("
  	sql = sql & request("seqnum") & ",'"
	if (request("TrType")="Verify") then
  	  sql = sql & "IRV','"
	  sql = sql & empCode & "','"
	end if
	if (request("TrType")="MultiVerify") then
  	  sql = sql & "IRV','"
 	  sql = sql & empCode & "','"
	end if
	if (request("TrType")="Approve") then
	  sql = sql & "IRA','"
	  sql = sql & empCode & "','"
	end if
	if (request("TrType")="Approve2") then
	  sql = sql & "IRA','"
	  sql = sql & empCode & "','"
	end if
	if (request("TrType")="PreApprove") then
	  sql = sql & "IRP','"
	  sql = sql & empCode & "','"
	end if
	if (request("TrType")="PreApprove2") then
	  sql = sql & "IRP','"
	  sql = sql & empCode & "','"
	end if
	if (request("TrType")="Route") then
	  sql = sql & "IRT','"        
 	  if (request("MultiUser")="Y") then
	  sql = sql & NextLocation & "','"
	  else
	  sql = sql & request("reqEndUser") & "','"
	  end if
	end if
  	sql = sql & request("Dept") & "',"
  	sql = sql &  "GETDATE(),'"
  	sql = sql & usrName & "',"
  	sql = sql & "GETDATE() )"
    cn.execute(sql)
	
	'----------------------------- after approve or preapprove invoice must be routed to other user
	
	if (request("TrType")="Verify") or (request("TrType")="MultiVerify") then
	sql = "INSERT INTO EXIHIST (SEQNUM,TRANSTYPE,LOCATION,DEPARTMENT,DATE,UPDATEUSER,UPDATESTAMP)"
  	sql = sql & " VALUES ("
  	sql = sql & request("seqnum") & ",'"
	sql = sql & "IRT','"
	if (request("TrType")="Verify") then
	 if (request("MultiUser")="Y") then
	 sql = sql & NextLocation & "','"
	 else
	 sql = sql & request("reqApproved") & "','"
	 end if
	end if
	if (request("TrType")="MultiVerify") then
	sql = sql & NextLocation & "','"
	end if
  	sql = sql & request("Dept") & "','"
  	sql = sql & request("RouteDate") & "','"
  	sql = sql & usrName & "',"
  	sql = sql & "GETDATE() )"
    cn.execute(sql)
	end if
	
	if (request("TrType")="PreApprove")or(request("TrType")="Approve")or(request("TrType")="PreApprove2")or(request("TrType")="Approve2") then
	sql = "INSERT INTO EXIHIST (SEQNUM,TRANSTYPE,LOCATION,DEPARTMENT,DATE,UPDATEUSER,UPDATESTAMP)"
  	sql = sql & " VALUES ("
  	sql = sql & request("seqnum") & ",'"
	sql = sql & "IRT','"
	if(request("TrType")="PreApprove") then
	  if (request("JoinAppr")="Y") then
	  sql = sql & request("Approved2") & "','"
	  else
 	  sql = sql & request("reqAuthorised") & "','"
	  end if
	end if
	if(request("TrType")="PreApprove2") then
 	  sql = sql & request("reqAuthorised") & "','"
	end if
	if(request("TrType")="Approve") then
	  if (request("JoinAppr")="Y") then
	  sql = sql & request("Approved2") & "','"
	  else
 	   if (request("refOrder")="Y") then
 	    sql = sql & request("UC") & "','" '"FINANCE','"
	    else
	    sql = sql & request("recBy") & "','" '"FINANCE','"
	    end if
	  end if
	end if
	if(request("TrType")="Approve2") then
 	   if (request("refOrder")="Y") then
 	    sql = sql & request("UC") & "','" '"FINANCE','"
	    else
	    sql = sql & request("recBy") & "','" '"FINANCE','"
	    end if
	end if
	if(request("TrType")="Approve")and(request("refOrder")="Y") then
	sql = sql &  "FINANCE','"
	else
  	sql = sql & request("Dept") & "','"
	end if
  	sql = sql & request("RouteDate") & "','"
  	sql = sql & usrName & "',"
  	sql = sql & "GETDATE() )"
    cn.execute(sql)
	end if

    '----------------------------------------------------------------------
	
'create email notification
    sCCList = ""
	if (request("TrType")="MultiVerify") then
	sRecipients = GetLoginName(NextLocation)
	end if
    if (request("TrType")="Verify")then
	    if (request("MultiUser")="Y") then
		sRecipients = GetLoginName(request("Ver1"))
	    else
		sRecipients = GetLoginName(request("reqApproved"))
		end if
	 'sRecipients = GetLoginName(request("reqApproved"))
	end if
	if (request("TrType")="PreApprove")then
	  if (request("JoinAppr")="Y") then
	  sRecipients = GetLoginName(request("Approved2"))
	  else
	  sRecipients = GetLoginName(request("reqAuthorised"))
	  end if
	end if
	if (request("TrType")="PreApprove2")then
	 sRecipients = GetLoginName(request("reqAuthorised"))
	end if
	if (request("TrType")="Route")then	 
	 if (request("MultiUser")="Y") then
	 sRecipients = GetLoginName(NextLocation)
	 else
	 sRecipients = GetLoginName(request("reqEndUser"))
	 end if
	end if
	if (request("TrType")="Approve")then
	  if (request("JoinAppr")="Y") then
	    sRecipients = GetLoginName(request("Approved2"))
	  else
	    if (request("refOrder")="Y") then
		   if (request("uc")="") then
	       sRecipients = GetLoginName(request("reqEndUser"))
		   else
		   sRecipients = GetLoginName(request("UC"))
		   end if
	    else
		  sRecipients = ""
		end if
	  end if
	end if
	
'create email subject and email body    
	sDomainName = "@Premier-oil.com"
	EmailSubject = "Electronic Invoice Approval "  
	EmailBody = "<HTML><HEAD><TITLE></TITLE></HEAD><BODY>"
	EmailBody = EmailBody & "<FONT face=Arial size=2>Dear Sir/Madame, </font><BR><BR>"
	if (request("TrType")="Approve") and (request("JoinAppr")="Y")then
	  	EmailBody = EmailBody & "<FONT face=Arial size=2>" & request("ApprName") & " has approved the following invoice: </font><BR><BR>"
		EmailBody = EmailBody & "<FONT face=Arial size=2>Vendor Name: " & request("VendorName") & "</font><BR>"
		EmailBody = EmailBody & "<FONT face=Arial size=2>Invoice No: " & request("InvoiceNo") & "</font><BR>"
		EmailBody = EmailBody & "<FONT face=Arial size=2><b><a href='" & theURL & "/My_InvApproval_mgr.asp?seqnum=" &  request("seqnum")  & "'>Click here for Invoice detail</a></b></font><BR><BR>"
		EmailBody = EmailBody & "<FONT face=Arial size=2>Thank you for your kind cooperation</font><BR><BR>"
		EmailBody = EmailBody & "<FONT face=Arial size=2><b>Finance Department</b></font>"
	elseif (request("TrType")="PreApprove") then
	    EmailBody = EmailBody & "<FONT face=Arial size=2>" & request("ApprName") & " has approved the following invoice: </font><BR><BR>"
		EmailBody = EmailBody & "<FONT face=Arial size=2>Vendor Name: " & request("VendorName") & "</font><BR>"
		EmailBody = EmailBody & "<FONT face=Arial size=2>Invoice No: " & request("InvoiceNo") & "</font><BR>"
		EmailBody = EmailBody & "<FONT face=Arial size=2><b><a href='" & theURL & "/My_InvApproval_mgr.asp?seqnum=" &  request("seqnum")  & "'>Click here for Invoice detail</a></b></font><BR><BR>"
		EmailBody = EmailBody & "<FONT face=Arial size=2>Thank you for your kind cooperation</font><BR><BR>"
		EmailBody = EmailBody & "<FONT face=Arial size=2><b>Finance Department</b></font>"
	elseif (request("TrType")="PreApprove2") then
	    EmailBody = EmailBody & "<FONT face=Arial size=2>" & request("ApprName2") & " has approved the following invoice: </font><BR><BR>"
		EmailBody = EmailBody & "<FONT face=Arial size=2>Vendor Name: " & request("VendorName") & "</font><BR>"
		EmailBody = EmailBody & "<FONT face=Arial size=2>Invoice No: " & request("InvoiceNo") & "</font><BR>"
		EmailBody = EmailBody & "<FONT face=Arial size=2><b><a href='" & theURL & "/My_InvApproval_mgr.asp?seqnum=" &  request("seqnum")  & "'>Click here for Invoice detail</a></b></font><BR><BR>"
		EmailBody = EmailBody & "<FONT face=Arial size=2>Thank you for your kind cooperation</font><BR><BR>"
		EmailBody = EmailBody & "<FONT face=Arial size=2><b>Finance Department</b></font>"
	elseif (request("TrType")="Verify") then
	    EmailBody = EmailBody & "<FONT face=Arial size=2>Please find Invoice for your review: </font><BR><BR>"
		EmailBody = EmailBody & "<FONT face=Arial size=2>Vendor Name: " & request("VendorName") & "</font><BR>"
		EmailBody = EmailBody & "<FONT face=Arial size=2>Invoice No: " & request("InvoiceNo") & "</font><BR>"
		EmailBody = EmailBody & "<FONT face=Arial size=2><b><a href='" & theURL & "/My_InvApproval_mgr.asp?seqnum=" &  request("seqnum")  & "'>Click here for Invoice detail</a></b></font><BR><BR>"
		EmailBody = EmailBody & "<FONT face=Arial size=2>Thank you for your kind cooperation</font><BR><BR>"
		EmailBody = EmailBody & "<FONT face=Arial size=2><b>Finance Department</b></font>"
	elseif (request("TrType")="Multiverify") and (NextLocation=request("reqApproved")) then
	    EmailBody = EmailBody & "<FONT face=Arial size=2>Please find Invoice for your review: </font><BR><BR>"
		EmailBody = EmailBody & "<FONT face=Arial size=2>Vendor Name: " & request("VendorName") & "</font><BR>"
		EmailBody = EmailBody & "<FONT face=Arial size=2>Invoice No: " & request("InvoiceNo") & "</font><BR>"
		EmailBody = EmailBody & "<FONT face=Arial size=2><b><a href='" & theURL & "/My_InvApproval_mgr.asp?seqnum=" &  request("seqnum")  & "'>Click here for Invoice detail</a></b></font><BR><BR>"
		EmailBody = EmailBody & "<FONT face=Arial size=2>Thank you for your kind cooperation</font><BR><BR>"
		EmailBody = EmailBody & "<FONT face=Arial size=2><b>Finance Department</b></font>"
	else
		EmailBody = EmailBody & "<FONT face=Arial size=2>Please find Invoice for your review: </font><BR><BR>"
		EmailBody = EmailBody & "<FONT face=Arial size=2>Vendor Name: " & request("VendorName") & "</font><BR>"
		EmailBody = EmailBody & "<FONT face=Arial size=2>Invoice No: " & request("InvoiceNo") & "</font><BR>"
		EmailBody = EmailBody & "<FONT face=Arial size=2><b><a href='" & theURL & "/My_InvApproval.asp?seqnum=" &  request("seqnum")  & "'>Click here for Invoice detail</a></b></font><BR><BR>"
		EmailBody = EmailBody & "<FONT face=Arial size=2>Thank you for your kind cooperation </font><BR><BR>"
		EmailBody = EmailBody & "<FONT face=Arial size=2><b>Finance Department </b></font>"
	end if
	
	EmailBody = EmailBody & "</BODY></HTML>"
	
	Set Email = Server.CreateObject("CDONTS.NewMail")
	Email.From = usrName & sDomainName
	Email.To = sRecipients 
	Email.cc = sCCList
	Email.BodyFormat = cdoBodyFormatHTML
	Email.Body = ReformatText(EmailBody)
	Email.Subject = EmailSubject
	Email.MailFormat = cdoMailFormatMIME
	if (sRecipients <> "") then
	Email.Send
	end if
	Set Email = Nothing

'back to approval page
set rsDOA=nothing
response.redirect "my_Invoice.asp"
else
response.redirect "my_InvApproval.asp?seqnum="& request("seqnum")
end if


%>
